#!/usr/bin/perl
#index.cgi

require './folderman-lib.pl';

&ReadParse();
my $dir = $in{'dir'};

my $mntroot = &get_mnt_root;
if (($dir ne "")&&($dir !~ /^$mntroot/))
{
	&nas_syslog("folderman", "naslog_warning", 30010, "Directory invalid!");
	&error($text{'dir_invalid'});
}
if (($dir ne "")&& !(-e $dir))
{
	&nas_syslog("folderman", "naslog_warning", 30011, "Directory invalid!");
	&error($text{'dir_invalid'});
}
	
$dir = "" if ($dir eq $mntroot);
if ($dir)
{
	&header($text{'folders_title'}, "", "index", 0, 1);
}
else
{
	&header($text{'volumes_title'}, "", "index", 0, 1);
}

print "<form name=editform action=edit_folder.cgi method=post onsubmit='disable_buttons();'>\n";


my $nd = &list_folders($dir);

print <<EOF;
<script>
function GetSelectedFolder(obj, n)
{
	var i;
	found = false;
	if (n >1)
	for(i = 0; i < document.editform.subdir.length; i++)
	{
		if (document.editform.subdir[i].checked)
		{
			found = true;
			break;
		}
	}
	else if (n == 1)
	{
		if (document.editform.subdir.checked)
			{
				found = true;
			}
	}

	if (found)
	{
		if (n > 1)
			document.editform.dir.value = document.editform.subdir[i].value;
		else
			document.editform.dir.value = document.editform.subdir.value;
	}
	else
		document.editform.dir.value = '';
	if (obj ==document.editform.newdir)
		found = true;
	return found;
}

function SetEditName(obj, n)
{
	if (GetSelectedFolder(obj, n))
	{
		if (obj ==document.editform.deldir)
		{
			if (!confirm("$text{'delete_folder_query'}"))
				return false;
		}
	}
	else
	{
		alert("$text{'noselectfolder'}");
		return false;
	}

	if (obj == document.editform.newdir)
		document.editform.edit.value = 'create'
	else if (obj == document.editform.sharedir)
		document.editform.edit.value = 'property'
	else if (obj == document.editform.editdir)
		document.editform.edit.value = 'property'
	else
		document.editform.edit.value = '';

	if (obj ==document.editform.deldir)
	{
		document.editform.action = 'delete_folder.cgi';
	}
	else if (obj == document.editform.sharedir)
	{
		document.editform.action = '../shareman/edit_share.cgi';
		document.editform.sharepath.value = document.editform.dir.value;
	}
	else
		document.editform.action = 'edit_folder.cgi';
	return true;
}
function GetSelectedNv(obj, n)
{
        var j;
        nfound = false;
        if (n >1)
        for(j = 0; j < document.editform.volname.length; j++)
        {
                if (document.editform.volname[j].checked)
                {
                        nfound = true;
                        break;
                }
        }
        else if (n == 1)
        {
                if (document.editform.volname.checked)
                        {
                                nfound = true;
                        }
        }

        if (nfound)
        {
                if (n > 1)
                        document.editform.netvol.value = document.editform.volname[j].value;
                else
                        document.editform.netvol.value = document.editform.volname.value;
        }
        else
                document.editform.netvol.value = '';
        if (obj ==document.editform.createnfs)
                nfound = true;
        return nfound;
}
function SetNvEditName(obj, n)
{
        if (GetSelectedNv(obj, n))
        {
                if (obj ==document.editform.deletenetvol)
                {
                        if (!confirm("$text{'delete_folder_query'}"))
                                return false;
                }
        }
        else
        {
                alert("$text{'noselectfolder'}");
                return false;
        }
        if (obj == document.editform.createnfs)
                document.editform.edit.value = 'create'
        else if (obj == document.editform.connectnetvol)
                document.editform.edit.value = 'property'
        else if (obj == document.editform.disconnetvol)
                document.editform.edit.value = 'property'
        else
                document.editform.edit.value = '';

        if (obj ==document.editform.createnfs)
        {
                document.editform.action = 'createnv.cgi';
        }
        else if (obj == document.editform.connectnetvol)
        {
                document.editform.action = 'connectnv.cgi';
                document.editform.netvol.value = document.editform.vol.value;
        }
	//else if (obj == document.editform.disconnetvol)
	//{
	//	document.editform.action = 'index.cgi';
	//}
        else
                document.editform.action = 'deletenv.cgi';
	return true;
}


function enter_dir(v)
{
	document.editform.action = 'index.cgi';
	document.editform.dir.value = v;
	document.editform.submit();
}

function disable_buttons()
{
	document.forms[0].newdir.disabled = true;
	document.forms[0].deldir.disabled = true;
	document.forms[0].editdir.disabled = true;
	document.forms[0].sharedir.disabled = true;
}
</script>
EOF


print "<input type=hidden name=edit>\n";
printf "<input type=hidden name=locate value='%s'>\n", ($dir)?$dir:$mntroot;
print "<input type=hidden name=dir><p>\n";
print "<input type=hidden name=netvol>\n";
print "<input type=hidden name=src value='disk'>\n";
print "<input type=hidden name=sharepath>\n";


#print "<input type=submit name=newdir value=\"$text{'create_folder'}\" onclick='return SetEditName(this, $nd)' class=button>\n" if ($dir);

#if ($nd)
#{

#	print "<input type=submit name=deldir value=\"$text{'delete_folder'}\" onclick='return SetEditName(this, $nd)' class=button>\n" if($dir);
#	print "<input type=submit name=editdir value=\"$text{'folder_property'}\" onclick='return SetEditName(this, $nd)' class=button>\n" if($dir);
#	print "<input type=submit name=sharedir value=\"$text{'share_property'}\" onclick='return SetEditName(this, $nd)' class=button>\n";
#}
print "</form>\n";
#print "</p>\n";

#print "<i>", $text{'attributes_hint'},"<p>", $text{'sharetypes_hint'}, "</i>\n";
#print "<hr>\n";
&footer("/", $text{'index'});


